package com.example.photography;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.example.photography.Activity.Appointment_Actvity;
import com.example.photography.Activity.Photo_Publish;
import com.example.photography.common.BaseActivity;
import com.example.photography.common.BaseFragment;
import com.example.photography.common.Constant;
import com.example.photography.fragment.CommunityFragment;
import com.example.photography.fragment.HomeFragment;
import com.example.photography.fragment.HomeFragment2;
import com.example.photography.fragment.MeFragment;
import com.example.photography.fragment.StoreFragment;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {
    @BindView(R.id.fl_content)
    FrameLayout flContent;
    @BindView(R.id.rg_main)
    RadioGroup rgMain;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    @BindView(R.id.rb_main_home)
    RadioButton rbMainHome;
    @BindView(R.id.rb_main_community)
    RadioButton rbMainCommunity;
    @BindView(R.id.publish)
    ImageView publish;
    @BindView(R.id.rb_main_store)
    RadioButton rbMainStore;
    @BindView(R.id.rb_mian_me)
    RadioButton rbMianMe;
    private long exitTime = 0;//退出时间变量值
    private List<BaseFragment> mBaseFragment;
    private CloudPushService mPushService;

    /**
     * 上次切换的Fragment
     */
    private Fragment mfragment;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return true;//拦截返回键
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            removeAll();
            System.exit(0);//销毁强制退出
        }
    }

    @Override
    protected void initData() {
        //绑定阿里推送账号
        mPushService = PushServiceFactory.getCloudPushService();
        mPushService.bindAccount(Constant.UID, new CommonCallback() {//绑定成功
            @Override
            public void onSuccess(String s) {
                Log.d("BindViewAccount:", "阿里云推送绑定成功");
            }
            @Override
            public void onFailed(String errorCode, String errorMsg) {
                Log.d("BindViewAccount:", "阿里云推送绑定失败");
            }
        });
        //初始化Fragment
        initFragment();
        //设置RadioGroup的监听
        setListener();
        ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
        View parentView = contentFrameLayout.getChildAt(0);
        if (parentView != null && Build.VERSION.SDK_INT >= 14) {
            parentView.setFitsSystemWindows(true);
        }
        //底部弹出框

        publish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 底部弹出框
                BottomSheetDialog sheelt = new BottomSheetDialog(MainActivity.this);
                View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.bottom_item, null);
                sheelt.setContentView(contentView);
                //给布局设置透明背景色
                sheelt.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(android.R.color.transparent);
                sheelt.show();
                ImageView iv_dismiss = contentView.findViewById(R.id.iv_dismiss);
                TextView tv_photo = contentView.findViewById(R.id.tv_photo);
                TextView tv_appoint = contentView.findViewById(R.id.tv_appoint);
                tv_photo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(MainActivity.this,Photo_Publish.class);
                        startActivity(intent);
                        sheelt.dismiss();
                    }
                });
                tv_appoint.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(MainActivity.this, Appointment_Actvity.class);
                        startActivity(intent);
                        sheelt.dismiss();
                    }
                });
                iv_dismiss.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        sheelt.dismiss();//消失
                    }
                });

            }
        });
    }

    private void setListener() {
        rgMain.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
        rgMain.check(R.id.rb_main_home);
    }

    /**
     * 选中的Fragment的对应的位置
     */
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            switch (checkedId) {
                case R.id.rb_main_home://首页
                    position = 0;
                    break;
                case R.id.rb_main_community://社区
                    position = 1;
                    break;

                case R.id.rb_main_store://商城
                    position = 2;
                    break;
                case R.id.rb_mian_me://我的
                    position = 3;
                    break;
                default:
                    position = 0;
                    break;
            }

            //根据位置得到对应的Fragment
            BaseFragment to = getFragment();
            //替换
            switchFrament(mfragment, to);
        }
    }

    /**
     * @param from 刚显示的Fragment,马上就要被隐藏了
     * @param to   马上要切换到的Fragment，一会要显示
     */
    private void switchFrament(Fragment from, Fragment to) {
        if (from != to) {
            mfragment = to;
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            //才切换
            //判断有没有被添加
            if (!to.isAdded()) {
                //to没有被添加
                //from隐藏
                if (from != null) {
                    ft.hide(from);
                }
                //添加to
                if (to != null) {
                    ft.add(R.id.fl_content, to).commit();
                }
            } else {
                //to已经被添加
                // from隐藏
                if (from != null) {
                    ft.hide(from);
                }
                //显示to
                if (to != null) {
                    ft.show(to).commit();
                }
            }
        }
    }

    /**
     * 根据位置得到对应的Fragment
     *
     * @return
     */
    private BaseFragment getFragment() {
        BaseFragment fragment = mBaseFragment.get(position);
        return fragment;
    }

    private void initFragment() {
        mBaseFragment = new ArrayList<>();
        mBaseFragment.add(new HomeFragment());
        mBaseFragment.add(new CommunityFragment());
        mBaseFragment.add(new StoreFragment());
        mBaseFragment.add(new MeFragment());
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void onBackPressed() {
        if (GSYVideoManager.backFromWindowFull(this)) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        GSYVideoManager.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
    }
}
